/*
 * v1.1 by tonydon
 * netbeansIDE 7.1
 */
package cn.txdnet.txweb.utils;

import java.io.Reader;
import java.sql.Clob;
import java.util.regex.Pattern;
import javax.servlet.http.HttpServletRequest;
import txdnet.util.Context;
import txdnet.util.DataUtil;
import txdnet.util.StaticMark;

/**
 *
 * @author txdnet
 */
public final class WebUtil {

    /*
     * 得到请求路径
     *     //http://hostname.com:80/mywebapp/servlet/MyServlet/a/b;c=123?d=789
     String scheme = aRequest.getScheme();             // http
     String serverName = aRequest.getServerName();     // hostname.com
     int serverPort = aRequest.getServerPort();        // 80
     String contextPath = aRequest.getContextPath();   // /mywebapp
     String servletPath = aRequest.getServletPath();   // /servlet/MyServlet
     String pathInfo = aRequest.getPathInfo();         // /a/b;c=123
     String queryString = aRequest.getQueryString();   // d=789

     // Reconstruct original requesting URL
     result = scheme + "://" + serverName + ":" + serverPort + contextPath + servletPath;
     */
    public static String getRequestUrl(String servletPath, String pathInfo) {
        return (pathInfo == null) ? servletPath : servletPath.concat(pathInfo);
    }

    public static String getUrlAction(String request_url) {
        return request_url.substring(0, request_url.lastIndexOf(StaticMark.CHAR_SLASH));
    }
    /*
     * 得到上下文路径
     */

    private static String getContextPath() {
        return Context.getServletContext().getContextPath();
    }

    /*
     * 得到网站部署目录路径
     */
    private static String getWebAppPath() {
        return Context.getRealPath(StaticMark.STR_SLASH);
    }

    /*
     * 得到网站上传临时目录
     */
    private static String getWebAppTmpDir() {
        return Context.getRealPath("/tmp");
    }

// 将CLOB转为String
    public static String parseClobToStr(Object clob) throws Exception {
        if (clob == null) {
            return StaticMark.STR_EMPTY;
        }
        StringBuilder sb = new StringBuilder(64);
        if (clob instanceof Clob) {
            char c[] = new char[1024];
            Reader reader = ((Clob) clob).getCharacterStream();
            while (reader.read(c, 0, 1024) != -1) {
                sb.append(c);
            }
        } else {
            sb.append(clob.toString());
        }
        return sb.toString().trim();
    }

    /*
     * @see 去掉注释
     */
    public static String clearNote(String str) {
        return Constants.RE_CLR_NOTE.matcher(str).replaceAll(StaticMark.STR_EMPTY);
    }

    /*
     * 去掉XSS代码,适用于文本框输入
     */
    public static String clearXss(String str) {
        return DataUtil.parseTxt2Html(str);
    }

// 得到URL BASENAME
    public static String getUrlMethod(String url) {
        String s = url.substring(url.lastIndexOf(StaticMark.CHAR_SLASH) + 1, url.length());
        int seg;
        if ((seg = s.indexOf(StaticMark.CHAR_DOT)) > 0) {
            s = s.substring(0, seg);
        }
        return s;
    }

    // 得到参数并进行验证
    public static String getParam(HttpServletRequest req, String param, String regexp, String retstr) {
        return DataUtil.getValid(req.getParameter(param), regexp, retstr);
    }

    public static String getParam(HttpServletRequest req, String param, Pattern p_regexp, String retstr) {
        return DataUtil.getValid(req.getParameter(param), p_regexp, retstr);
    }

    public static byte getByte(HttpServletRequest req, String param, int retVal) {
        String val = req.getParameter(param);
        if (DataUtil.isInteger(val) && val.length() <= 2) {
            return Byte.parseByte(val);
        } else {
            return (byte)retVal;
        }
    }

    public static short getShort(HttpServletRequest req, String param, int retVal) {
        String val = req.getParameter(param);
        if (DataUtil.isInteger(val) && val.length() <= 4) {
            return Short.parseShort(val);
        } else {
            return (short)retVal;
        }
    }

    public static int getInt(HttpServletRequest req, String param, int retVal) {
        String val = req.getParameter(param);
        if (DataUtil.isInteger(val) && val.length() <= 9) {
            return Integer.parseInt(val);
        } else {
            return retVal;
        }
    }

    public static long getLong(HttpServletRequest req, String param, long retVal) {
        String val = req.getParameter(param);
        if (DataUtil.isInteger(val) && val.length() <= 18) {
            return Long.parseLong(val);
        } else {
            return retVal;
        }
    }

    public static double getDouble(HttpServletRequest req, String param, String retstr) {
        return Double.parseDouble(WebUtil.getParam(req, param, Constants.RE_DOUBLE, retstr));
    }

    public static String getNick(HttpServletRequest req, String param, String retstr) {
        return WebUtil.getParam(req, param, Constants.RE_NICK, retstr);
    }

    public static String getTitle(HttpServletRequest req, String param, String retstr) {
        return WebUtil.getParam(req, param, Constants.RE_TITLE, retstr);
    }

    public static String getArrStr(HttpServletRequest req, String param, String retstr) {
        return WebUtil.getParam(req, param, Constants.RE_STRARR, retstr);
    }

    public static String getEmail(HttpServletRequest req, String param, String retstr) {
        return WebUtil.getParam(req, param, Constants.RE_EMAIL, retstr);
    }

    public static String getTel(HttpServletRequest req, String param, String retstr) {
        return WebUtil.getParam(req, param, Constants.RE_TEL, retstr);
    }

    public static String getNoNull(HttpServletRequest req, String param, String retstr) {
        return DataUtil.checkNull(req.getParameter(param), retstr);
    }
    
    /**
     * web app 绝对路径
     */
    public final static String WEB_APP_PATH = WebUtil.getWebAppPath();
    /**
     * web context 上下文路径
     */
    public final static String WEB_CTX_PATH = WebUtil.getContextPath();
    /**
     * web app 临时文件夹路径
     */
    public final static String WEB_APP_TMPDIR = WebUtil.getWebAppTmpDir();
}